<template>
  <div class="view-wrapper">
    <div class="head">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="区域名称：">
          <el-input v-model="formInline.name" placeholder="请输入区域名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">查询</el-button>
          <el-button @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="listBox">
      <div class="listBox_head">
        <el-button type="primary" @click="addBuy">添加区域</el-button>
      </div>
      <ex-table ref="table" :loading="loading" :table-column="tableColumn" :table-data="tableData"
        @pageSizeChange="handleSizeChange" @currentPageChange="handleCurrentChange">
        <template v-slot:areaList="{ scope }">
          <!-- <div v-for="(item,index) in scope.row.areaList" :key="index">{{item}}</div> -->
          <el-popover placement="top-start" title="" width="200" trigger="hover" content="">
            <div v-for="(item,index) in scope.row.areaList" :key="index">{{item}}</div>
            <div class="area_box" slot="reference">{{scope.row.areaList[0]}}<span v-if="scope.row.areaList.length>1">......</span></div>
          </el-popover>
        </template>
        <template v-slot:operate="{ scope }">
          <el-button type="primary" size="small" @click="editJump(scope.row.id)">编辑</el-button>
        </template>
      </ex-table>
    </div>
    <!-- 添加区域弹窗 -->
    <detail-dialog v-if="detailDialog" :id="editId" :visible.sync="detailDialog" @success="_handleSuccess" />
  </div>
</template>
<script>
  // components
  import {
    getAreaList,
  } from "@/api/setting";
  import ExportExcelButton from "@/components/ExportExcelButton/index";
  import handlePageMixin from "@/mixins/handlePageMixin";
  import DetailDialog from "./components/regionSetting/DetailDialog";
  import {
    mapGetters
  } from "vuex";
  export default {
    name: "billList",
    components: {
      ExportExcelButton,
      DetailDialog
    },
    mixins: [handlePageMixin],
    data() {
      return {
        loading: false,
        formInline: {
          name: "", //区域名称
        },
        detailDialog: false,
        ruleForm: {
          name: "", //区域名称
          sort: "", //推荐序号
        },
        rules: {
          name: [{
            required: true,
            message: "请输入区域名称",
            trigger: "blur"
          }, ],
          sort: [{
            required: true,
            message: "请输入排序号",
            trigger: "blur"
          }, ],
        },
        tableData: [],
        tableColumn: [{
            prop: "name",
            label: "区域名称",
          },
          {
            label: "管辖区域",
            slot: true,
            slotName: "areaList",
            width: 280
          },
          {
            prop: "coolingOffDay",
            label: "冷静期",
          },
          {
            prop: "sort",
            label: "排序",
            width: 120
          },
          {
            label: "操作",
            slot: true,
            slotName: "operate",
            width: 240,
            fixed: "right",
          },
        ],
        shopId: '',
        editId: '', //编辑id
      };
    },
    computed: {
      ...mapGetters(["isChain"]),
    },
    created() {},
    mounted() {
      this.getList();
    },
    methods: {
      addBuy() {
        //新增
        this.title = '添加区域'
        this.ruleForm = {
            shopObject: '', //店铺下拉对象，后端不做处理
            grouponId: "", //团购id
            module: "", //推荐模块 1 爆品推荐 2 今日上新  字典值查询参数 merchant_groupon_recommend_module
            shopId: "", //店铺id
            sort: "", //推荐序号
          },
          this.editId = null
        this.detailDialog = true
      },
      editJump(id) {
        //编辑
        this.title = '编辑区域'
        this.editId = id
        this.detailDialog = true
      },
      _handleSuccess(e) {
        //成功回调
        this.reset()
      },
      search() {
        //搜索
        console.log(this.formInline);
        this.pageOptions.pageNum = 1;
        this.$refs.table.pageOptions.pageNum = 1;
        this.getList();
      },
      reset() {
        this.formInline = {};
        this.pageOptions.pageNum = 1;
        this.$refs.table.pageOptions.pageNum = 1;
        this.getList();
      },
      getList() {
        const params = {
          ...this.pageOptions,
          ...this.formInline,
        };
        this.loading = true;
        getAreaList(params)
          .then((res) => {
            const {
              total,
              records
            } = res.result;
            records.forEach((item) => {
              if(item.coolingOffType=='1'){
                item.coolingOffDay = '无限冷静期'
              }
            });
            this.tableData = records;
            this.$refs.table.pageOptions.total = total;
          })
          .finally(() => {
            this.loading = false;
          });
      },
    },
  };

</script>
<style lang="scss" scoped>
  .el-card-title {
    font-size: 20px;
  }

  .head {
    //   box-shadow: 0px 4px 12px 0px rgb(1 41 99 / 6%);
    background-color: #fff;
    border-radius: 8px;
    //   height: 120px;
    padding: 20px 20px 0;
  }

  .el-form--inline .el-form-item {
    margin-right: 40px;
  }

  .listBox {
    //   box-shadow: 0px 4px 12px 0px rgb(1 41 99 / 6%);
    background-color: #fff;
    border-radius: 8px;
    //   height: 120px;
    padding: 20px 20px;
    margin-top: 10px;
  }

  .table_list {
    display: flex;
    align-items: center;
    // border: 1px solid #DCDFE6;
    border-radius: 7px;
    width: 220px;
    margin-bottom: 10px;
  }

  .tableBox {
    width: calc(100% / 3);
    height: 36px;
    text-align: center;
    line-height: 36px;
    color: #303133;
    font-size: 14px;
    border: 1px solid #dcdfe6;
    border-right: 0;
  }

  .tableBox:first-child {
    border-radius: 7px 0px 0px 7px;
  }

  .tableBox:last-child {
    border-right: 1px solid #dcdfe6;
    border-radius: 0px 7px 7px 0px;
  }

  .spchoosetext {
    background-color: #fff4e6;
    color: #ff9300;
    // border: 1px solid #ff9300;
  }

  .listBox_head {
    margin-bottom: 20px;
    display: flex;
  }
  .area_box{
    cursor: pointer;
  }
  .area_box:hover{
    color: #1890FF;
  }
</style>
